$(function() {
    init();

    function init() {
        mui.init({
            pullRefresh: {
                container: ".view", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
                // 下拉
                down: {
                    auto: true, //可选,默认false.首次加载自动下拉刷新一次
                    callback: function() {
                            getQueryProductDetail(function() {
                                mui('.view').pullRefresh().endPulldownToRefresh();
                            });
                        } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
                }
            }
        });
        eventList();
    }

    function eventList() {
        $(".content").on("tap", ".pro_size", function() {
            $(this).addClass("active").siblings().removeClass("active");
        })
        $(".add_cart").on("tap", function() {
            var sizeBtnlength = $(".pro_size.active").length;
            if (sizeBtnlength == 0) {
                mui.toast("请选择码数");
                return;
            }
            var params = {
                productId: $.getQueryString("productId"),
                size: $(".pro_size.active").text(),
                num: $(".num_val").val(),
            }
            $.post(location.origin + "/cart/addCart", params, function(ret) {
                if (ret.error && ret.error == 400) {
                    mui.toast(ret.message);
                    setTimeout(function() {
                        sessionStorage.setItem("pageName", location.href);
                        location.href = "./login.html";
                    }, 1000);
                } else if (ret.success) {
                    mui.confirm("添加成功,是否要跳转页面", "提示", ["是", "否"], function(etype) {
                        if (etype.index == 0) {
                            location.href = "./cart.html";
                            // 是
                        } else if (etyp.index == 1) {
                            // 否
                        }
                    })
                }
            })
        })
    }

    function getQueryProductDetail(callback) {
        var id = $.getQueryString("productId");
        $.get(location.origin + "/product/queryProductDetail?id=" + id, function(ret) {
            // console.log(ret);
            ret.sizeArr = $.formatSize(ret.size);
            console.log(ret);
            var html = template("conTemp", ret);
            $(".content").html(html);
            var gallery = mui('.mui-slider');
            gallery.slider({
                interval: 5000 //自动轮播周期，若为0则不自动播放，默认为0；
            });
            mui(".mui-numbox").numbox();
            callback && callback();
        })
    }
})